use std::io;

use steamworks::Client;

use crate::{
    models::command::UGCCommands,
    ugc::{
        action::{download_item, subscribe_item, subscribed_items, unsubscribe_item},
        query::{get_all, get_collections, get_item, get_items},
        state::{get_item_state, item_download_info},
    },
    utils::response::DataResponse,
};

// 处理UGC命令
pub fn handle_ugc_commands(action: &UGCCommands, client: Client) {
    match action {
        UGCCommands::Item { id } => {
            let response = get_item(*id, client.clone())
                .map(|item| DataResponse::success(Some(item)))
                .unwrap_or_else(|e| DataResponse::error(None, e));
            let _ = serde_json::to_writer(io::stdout(), &response);
        }
        UGCCommands::Items { ids } => {
            let ids: Vec<u64> = ids.split(',').map(|s| s.parse::<u64>().unwrap()).collect();
            let response = get_items(ids.clone(), client.clone())
                .map(|items| DataResponse::success(Some(items)))
                .unwrap_or_else(|e| DataResponse::error(None, e));
            let _ = serde_json::to_writer(io::stdout(), &response);
        }
        UGCCommands::All { page } => {
            let app = client.utils().app_id().0;
            let response = get_all(*page, app, client.clone())
                .map(|data| DataResponse::success(Some(data)))
                .unwrap_or_else(|e| DataResponse::error(None, e));
            let _ = serde_json::to_writer(io::stdout(), &response);
        }
        UGCCommands::Collections { page } => {
            let app = client.utils().app_id().0;
            let response = get_collections(*page, app, client.clone())
                .map(|data| DataResponse::success(Some(data)))
                .unwrap_or_else(|e| DataResponse::error(None, e));
            let _ = serde_json::to_writer(io::stdout(), &response);
        }
        UGCCommands::DownloadInfo { id } => {
            let res = item_download_info(*id, client.clone());
            let _ = serde_json::to_writer(io::stdout(), &DataResponse::success(res));
        }
        UGCCommands::State { id } => {
            let response = get_item_state(*id, client.clone())
                .map(|status| DataResponse::success(Some(status)))
                .unwrap_or_else(|e| DataResponse::error(None, e));
            let _ = serde_json::to_writer(io::stdout(), &response);
        }
        UGCCommands::Subscribe { id } => {
            let res = subscribe_item(*id, client.clone());
            let _ = serde_json::to_writer(io::stdout(), &res);
        }
        UGCCommands::Subscribed => {
            let res = subscribed_items(client.clone());
            let _ = serde_json::to_writer(io::stdout(), &res);
        }
        UGCCommands::Unsubscribe { id } => {
            let res = unsubscribe_item(*id, client.clone());
            let _ = serde_json::to_writer(io::stdout(), &res);
        }
        UGCCommands::Download { id } => {
            let res = download_item(*id, client.clone());
            let _ = serde_json::to_writer(io::stdout(), &res);
        }
    }
}
